This document contains:

Completing the exercise will familiarize you with some simple yet important basic concepts of R - knowing the basics will allow you to focus on the much more interesting things (like plotting graphs) in the workshop, leading to a more rewarding experinece.

Importantly, if something went wrong and you could not install the software, turn up 10 minutes before the workshop so we can try to troubleshoot. Since the workshop is going to be short and intensive, we cannot afford to waste any time on issues like installation.

1 Installing the software

You must have two pieces of (free) software installed on you laptop (which you should bring with you) before coming to the workshop - the event is too short to allow us to spend time on installation. The installation process is very simple.

1.1 Installing R

First and foremost, you need R. If you have R installed already, update it to the most recent version (which is done just by downloading and installing the most recent installer). Depending on your operating system, go to:

Download the installer and install (with default options, just keep clicking Next). Run the R once to see that it works (in Windows, Rgui.exe should appear as a shortcut in the start menu and/or desktop; on a Mac, look for the R application in Finder). It should look something like:

on a Mac

on Windows

Good job. Now close R (if it asks to save the workspace: no need for that). Once you get RStudio, there is no need to look at this ugly interface ever again.

1.2 Installing RStudio

While it is completely fine to use R from the command line or the bare-bones R interface application, we are going to use RStudio, which will make using R a lot easier and less of a hassle. It also has nice support for R Markdown, which we will be using. Go to:

Download and install. Run RStudio (again, look for the shortcut in Start Menu/Finder or the desktop).

RStudio is an integrated development environment for R (which is why we had to install that first) - the Console panel on the left is basically the same thing that you saw when you ran “plain” R. But RStudio also features a number of very helpful things that will become apparent in the workshop. Before the workshop, you are invited to complete a short exercise. RStudio comes with a handy script editor, which we are going to use right away.

Before we do that, we need to change one interface option in RStudio. Go to Tools -> Global Options, choose the Code tab on the left, and tick “Soft-wrap R source files” (this will make using the script editor much easier, by wrapping long lines so you won’t have to keep scrolling left and right all the time).

2 The pre-workshop exercise (super-duper mandatory)

Time for a quick exercise on the basics of R. Below is a block of code. What you need to do is select and copy this block of code, and paste it into the RStudio script editor. First create a new script by clicking on the New icon in the top left corner of RStudio (or File->New File) and choose “R Script”.

A blank text file will open above the Console panel, titled something like Untitled1.R. Your RStudio window should look something like this:

Good job! Now copy-paste the code below (in the gray box) into the new script file you just created, and follow the instructions therein.


# Start of the exercise. You should be reading this message in the RStudio script editor.
print("Hello! Put your text cursor on this line (click on the line). Anywhere on the line. Now press CTRL+ENTER (PC) or CMD+ENTER (Mac). Just do it.")

# The command above, when executed (what you just did), printed the text in the console below. Also, this here is a comment. Commented parts of the script (anything after a # ) are not executed.
# Also, if you've been scrolling left and right in the script window to read all this, turn on text wrapping ASAP: on the menu bar above, go to Tools -> Global Options -> Code (on the left) -> tick "Soft-wrap R source files". Better, right?

# So, print() is a function. Most functions look something like this: 
#      myfunction(inputs, parameters) 
# All the inputs to the function go inside the ( ) brackets. In the above case, the text (all text, or "strings", must be within quotes) is the input to the print() function.

# Let's try another function, sum().
sum(1,10) # cursor on the line, press CTRL+ENTER (or CMD+ENTER on Mac)
# You should see the output (sum of 1 and 10) in the console. Note that you can also write commands directly in the console, and executing them with ENTER. Try some simple maths: write 2*3+1 in the console below, and press ENTER.

# Important: you can always get help for a function and check its input parameters by executing 
help(sum)  # put the name of any function in the brackets
# ...or by searching for the function by name in the Help tab on the right.

# Let's plot something. The command for plotting is, surprisingly, plot().
plot(42, main = "Greatest plot in the world") # execute the command; a plot should appear on the right.
# OK, this was not very exciting. But notice that a function can have multiple inputs, or arguments. In this case, the first argument is the data (a vector of length one), and the second is 'main', which specifies the main title of the plot. 
# Note that you can make to plot bigger by pressing the 'Zoom' button above the plot panel on the right.

# Let's create some data to play with. We'll use the sample() command, which creates random numbers from a predifined sample. Basically it's like rolling a dice some n times, and recording the results.
sample(x = 1:6, size = 50, replace = T) # execute this; its output is 50 numbers 
# If an output is not assigned to some object, it usually just gets printed in the console. It would be easier to work with data, if we saved it in an object. For this, we need to learn assignement, which in R works using the equals = symbol (or the <-, but let's stick with = for simplicity).
xdata = sample(x = 1:6, size = 50, replace = T)  # what it means: xdata is the name of a (new) object, the equals sign (=) signifies assignement, with the object on the left and the data on the right. In this case, the data is the output of the sample() function. Instead of printing in the console, the output is assigned to the object.
xdata # execute to inspect: calling an object usually prints its contents into the console below.
# Let's plot:
plot(xdata)  # plots data as it is ordered in the object
hist(xdata, breaks=20, main="Frequency of dice values") # plots a histogram (distribution of values)

# One final thing and we're done. We need to make sure your RStudio and certain packages get along so we can use R Markdown in the workshop. During this process, RStudio might need to download a few things - make sure you have internet access. Read through the following instructions (1-6) first, and then follow them starting with 1. 
# 1. Click on New File  (either in the menu, or white button in the top left corner), and choose "R Markown...". 
# 2. At this point, RStudio will probably ask you to install some packages. Click "Yes". This will take some time (depending on your internet speed). This only happens the first time you try to use R Markdown.
# 3. When this is done, a new window will appear, titled "New R Markdown...". Just click "OK" to create the default document.
# 4. A new script file tab should appear in the script editor (right to your exercise script), probably titled "Untitled2.Rmd". It has some example contents. 
# 5. Save the new .Rmd file (click on the little blue save icon, give it some name and save).
# 6. Now click on the little "Knit" icon (with the blue ball of yarn) on top of the script panel. A new window should appear, containing a simple webpage, titled "Untitled", telling you that "This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML..." 
# Feel free to close this window now, you've completed the little exercise. If all this worked, great! If not, come to the workshop 10 minutes early so we can fix this.

# Well done!! This is the end of the pre-workshop tutorial. Congratulations, you have successfully installed R and RStudio, and have now learned the basics of programming (functions, assignement, parameters, input-output; as well as authoring basic webpages) - and as such could get a job in IT that would pay way better than whatever you might have planned as a career. Just kidding. 
# (mostly) You have also learned how to use the basic plot() command in R. This will form the basis of the workshop, where we will learn how to recognize ways to plot different kinds of data (not just random dice rolls!), and how to modify the plotting parameters in order to create informative but also visually more pleasing graphs (beyond the plain black-and-white examples like the above). 
# Best of all, all the code is reusable in the sense that you can easily use the very same commands that we will be working with to plot your own data later on, just by changing the inputs.
# Also, the "programming" side of things will not get any more complicated than what you did in the last 10 minutes. That is literally all you need - but we will explore how to use these basic skills in various different ways.





By the way, these are the sort of things you will be easily able to create after participating in the workshop + following the post-workshop bonus sections: